package com.faith.dynamic.aspect;

import com.faith.dynamic.annotation.FaithSchedule;
import com.faith.dynamic.enums.DynamicOperationType;
import com.faith.dynamic.task.DynamicScheduledTask;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.List;


/**
 * @ClassName DynamicHandler$
 * @Description 切面处理类
 * @Author faith$
 * @Date 2019/8/9 17:08
 * @Version 1.0
 **/
@Aspect
@Component
@Order(0)
public class DynamicHandler {
    private static final Logger logger = LoggerFactory.getLogger(DynamicHandler.class);

    @Autowired
    private DynamicScheduledTask task;

    @Pointcut("@annotation(faithSchedule)")
    public void dynamicPointCut(FaithSchedule faithSchedule) {

    }
    @Before("@annotation(faithSchedule)")
    public void doBefore(JoinPoint joinPoint,FaithSchedule faithSchedule) throws Throwable {
        if(!StringUtils.isEmpty(faithSchedule.scheduleMethod())) {
            faithSchedule.dynamicOperationType().handle(joinPoint, faithSchedule);
        }
    }
    @Around("dynamicPointCut(faithSchedule)")
    public Object doAround(ProceedingJoinPoint joinPoint,FaithSchedule faithSchedule) throws Throwable {
        Object ob = joinPoint.proceed();
        if(StringUtils.isEmpty(ob) && DynamicOperationType.LIST.name().equals(faithSchedule.dynamicOperationType().name())){
            List<String> handle = faithSchedule.dynamicOperationType().handle(joinPoint, faithSchedule);
            ob = handle;
        }
        return ob;
    }
}
